package com.elinkway.infinitemovies.download;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.elinkway.infinitemovies.utils.Utils;
import com.le123.ysdq.R;

import java.text.DecimalFormat;


/**
 * 下载模块的sdCard容量管理
 * 
 * @author daipei
 * 
 */
public class ContainSizeManager {

	// 容量相关：
	// 视图
	private TextView sdcardView; // 视图
	// 数据
	//private double funshionSize, freeSize; // 总容量，其他程序容量，风行容量，剩余大小(精度：2位,单位：G)
	private String  kuaikanStr, freeStr; // (对应上面的数据的字符串表示)
	private DecimalFormat df;
	private Activity view;
	private  double free;
	private double kuaikan;

	public ContainSizeManager(Activity view) {
		df = new DecimalFormat();
		df.setMinimumFractionDigits(2);
		df.setMaximumFractionDigits(2);
		this.view = view;
	}
	
	public ContainSizeManager() {
		df = new DecimalFormat();
		df.setMinimumFractionDigits(2);
		df.setMaximumFractionDigits(2);
	}


	// SDCard剩余空间大小（单位：GB，2位精度）
	public void countFreeSize() {
		String path = DownloadHelper.getDownloadPath().replace("/"+Utils.getDownLoadFolder(), "");
		free = DownloadHelper.getSdcardStorage(path) / 1024;
		if(df != null)
		freeStr = df.format(free);
	}
	
	// SDCard剩余空间大小（单位：GB，2位精度）
		public void countFreeSizeForSetting(String path) {
			free = DownloadHelper.getSdcardStorage(path.replace("/"+Utils.getDownLoadFolder(), "")) / 1024;
			if(df != null)
			freeStr = df.format(free);
		}

	// SDCard\kuaikan程序所占的空间大小（单位：GB，2位精度）
	public void countFunshionSize() {
		String path = DownloadHelper.getDownloadPath().replace("/"+Utils.getDownLoadFolder(), "");
		kuaikan = DownloadHelper.getSdUsedStorage(path)/1024;		
		if(df != null)
		kuaikanStr = df.format(kuaikan);
	}
	/**
	 * 获得sd卡余量
	 * @author daipei
	 */
	public double getFreeSize(){
		countFreeSize();
		return free*1024;
		
	}
	/**
	 * 获得sd卡余量
	 * 剩余空间大于1024MB，显示GB
	 * 否则。显示MB
	 * @author daipei
	 */
	public String getFreeSizeForSetting(String path){
		countFreeSizeForSetting(path);
		String freesize = free>=1 ? freeStr+"GB": Double.parseDouble(freeStr)*1024+"MB";
		return freesize;
		
	}

	public void ansynHandlerSdcardSize() {
		anysnUpdateSizeView();
	}

	public void anysnUpdateSizeView() {
		new Thread() {
			public void run() {
				// 取得相关值
				countFunshionSize();
				countFreeSize();
				ansynUpdateHandler.post(ansynUpdateUi);
			}
		}.start();
	}

	Runnable ansynUpdateUi = new Runnable() {

		@Override
		public void run() {
			// 找到视图并赋值
			sdcardView = (TextView) view.findViewById(R.id.available_space_tv);
			if (null != sdcardView) {
				sdcardView.setText("已用" +kuaikanStr+ "G/"+"可用"+freeStr+"G");
			}
			ProgressBar  progressBar = (ProgressBar)view.findViewById(R.id.available_space_bar);
			if (null != progressBar&& (kuaikan+free)>0) {
				double percentage = kuaikan/(kuaikan+free);
				if (0.0 == percentage) {
					if (free == 0.0) {
						percentage = 10.0;
					}
				}
				progressBar.setProgress((int)(percentage*100));
			} else {
				progressBar.setProgress(0);
			}
		}
	};

	// 处理容量更新的Handler
	@SuppressLint("HandlerLeak")
	Handler ansynUpdateHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
		}
	};

}
